package cn.tuling.standard;

public class DecoratorTest {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        component.operate();

        System.out.println("---------装饰器----------");

        Component enhancedComponent = new ConcreteDecorator(component);
        enhancedComponent.operate();
    }
}

interface Component {
    void operate();
}

class ConcreteComponent implements Component {
    public void operate() {
        System.out.println("被装饰对象方法执行");
    }
}

abstract class Decorator implements Component {
    private Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void operate() {
        component.operate();
    }
}

class ConcreteDecorator extends Decorator {

    public ConcreteDecorator(Component component) {
        super(component);
    }

    @Override
    public void operate() {
        super.operate();
        addFunction();
    }

    public void addFunction() {
        System.out.println("增加额外功能");
    }
}

